#!/bin/bash
#auto install lamp
#SOFTWARE DIR
SOFTDIR="/usr/local/src"
#APR
APR_SOFT="apr-1.6.3.tar.gz"
APR_SOFT_DIR="apr-1.6.3"
APR_URL="http://soft.51yuki.cn/"
APR_PREFIX="/usr/local/apr"
#APR-Util
APR_UTIL_SOFT="apr-util-1.6.1.tar.gz"
APR_UTIL_SOFT_DIR="apr-util-1.6.1"
APR_UTIL_URL="http://soft.51yuki.cn/"
APR_UTIL_PREFIX="/usr/local/apr-util"

#httpd define path variable
H_SOFT="httpd-2.2.34.tar.bz2"
H_SOFT_DIR="httpd-2.2.34"
H_URL="https://mirrors.cnnic.cn/apache/httpd/"
H_PREFIX="/usr/local/apache"

#MySQL define path variable
M_SOFT="mysql-5.6.39.tar.gz"
M_SOFT_DIR="mysql-5.6.39"
M_URL="https://cdn.mysql.com//Downloads/MySQL-5.6/"
M_PREFIX="/usr/local/mysql"

#php define path variable

P_SOFT="php-5.6.33.tar.bz2"
P_SOFT_DIR="php-5.6.33"
P_URL="http://mirrors.sohu.com/php/"
P_PREFIX="/usr/local/php5"

echo -e "\033[32m------------------------------\033[0m"
echo
if [ -z "$1"];then
	echo -e "\033[36mPlease Select Install Menu follow:\033[0m"
	echo -e "\033[32m1)编译安装apache服务器\033[0m"
	echo -e "\033[32m2)编译安装Mysql服务器\033[0m"
	echo -e  "\033[32m3)编译安装PHP服务器\033[0m"
	echo -e "\033[31mUsage: {/bin/bash $0 1|2|3|4|help } \033[0m"
	exit
fi

if [[ "$1" -eq "help" ]];then
	        echo -e "\033[36mPlease Select Install Menu follow:\033[0m"
        echo -e "\033[32m1)编译安装apache服务器\033[0m"
        echo -e "\033[32m2)编译安装Mysql服务器\033[0m"
        echo -e  "\033[32m3)编译安装PHP服务器\033[0m"
        echo -e "\033[31mUsage: {/bin/bash $0 1|2|3|4|help } \033[0m"
        exit
fi

#######################################################################
#Install httpd web server
if [ "$1" -eq "1" ];then
   #install apr
   cd $SOFTDIR && wget ${APR_URL}/${APR_SOFT} && tar xf ${APR_SOFT} && cd ${APR_SOFT_DIR} &&
./configure --prefix=${APR_PREFIX}  && make && make install
  cd ..
  #install apr-util
  wget ${APR_UTIL_URL}/${APR_UTIL_SOFT} && tar xf ${APR_UTIL_SOFT} && cd ${APR_UTIL_SOFT_DIR} && ./configure --prefix=${APR_UTIL_PREFIX}  --with-apr=${APR_PREFIX} && make && make install
 cd ..
 #install http
	cd $SOFTDIR && wget  ${H_URL}/${H_SOFT} && tar -xf ${H_SOFT} && ${H_SOFT_DIR} && \
	./configure --prefix=${H_PREFIX}  --with-apr=${APR_PREFIX} --with-apr-util=${APR_UTIL_PREFIX} --enable-include --enable-remoteip --with-zlib --with-pcre --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-mpm=worker -enable-modules=all 
	if [ $? -eq 0 ];then
		make && make install
	fi
fi
# install mysql server
#define mysql variables
USER="mysql"
DATADIR="/data/mysql"
if [ "$1" -eq "2" ];then
        #Install mysql prerequisites
	yum -y install ncurses-devel cmake make perl gcc autoconf automake zlib zlib-devel libxml libxml-devel bison-devel
	#create mysql user
	if ! id $USER &>/dev/null ;then
		groupadd -g 3306 mysql
		useradd -u 3306 -g mysql -s /sbin/nologin -r mysql
	fi
	#create mysql directory
	if [ ! -d $DATADIR ];then
		mkdir -p $DATADIR
	fi
	#download mysql software
	cd $SOFTDIR && wget ${M_URL}/${M_SOFT} && tar xf ${M_SOFT} && cd ${M_SOFT_DIR} && \
	cmake -DCMAKE_INSTALL_PREFIX=${M_PREFIX} -DMYSQL_DATADIR=${DATADIR} -DSYSCONFDIR=/etc/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
	-DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock  -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 \
	-DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci
       if [ $? -eq 0 ];then
		make && make install
       fi
       #configure mysql server
       chown -R $USER:$USER ${DATADIR}
       cd ${M_PREFIX} && scripts/mysql_install_db --basedir=${M_PREFIX} --datadir=${DATADIR} --user=$USER
       cp support-files/mysql.server /etc/init.d/mysqld
       chmod u+x /etc/init.d/mysqld
       chkconfig mysql on
       mv /etc/my.cnf{,.ori} && cp support-files/my-default.cnf /etc/my.cnf  
       cat >> /etc/security/limits.d/80-mysql.conf <<EOF

mysql soft nofile 40960
mysql hard nofile 40960
EOF
      # configure path env
      echo "export PATH=${M_PREFIX}/bin:$PATH" >> /etc/profile.d/mysql.sh
      source /etc/profile.d/mysql.sh
      #start mysql server
      service mysqld start 
fi
 
